<template>
  <div>
    <div style="margin-top: -90px;margin-bottom: -20px">政策</div>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px">
      <div style="margin-right: 125px;margin-left: 55px">
        <el-form-item label="政策标题" prop="title" >
          <el-input v-model="ruleForm.title"></el-input>
        </el-form-item>
        <el-form-item label="政策内容" prop="message">
          <el-input type="textarea" v-model="ruleForm.message"></el-input>
        </el-form-item>
        <el-form-item label="城市" prop="selectCity">
          <el-cascader
              style="width: 100%;"
              v-model="selectCity"
              :options="options"
              @change="handleChange"></el-cascader>
        </el-form-item>
      </div>
      <el-form-item style="margin-left: -90px">
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import {getCity,addPolicy} from "@/api/policy";

export default {
  data() {
    return {
      ruleForm: {
        title:'',
        message:'',
        selectCityId:''
      },
      selectCity:[],
      options:[],

      rules: {
        title: [{ required: true, message: '请输入政策标题', trigger: 'blur' },],
        message: [{ required: true, message: '请输入政策内容', trigger: 'blur' },]
      }
    };
  },
  created() {
    this.initSelectData()
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          addPolicy(this.ruleForm).then(res=>{
            if(res.code==200){
              //添加成功
              this.$message({
                type:"success",
                message:res.msg
              })
              //关闭添加窗口
              this.$emit("closeAddDialog")
            }else{
              //添加失败
              this.$message({
                type:"error",
                message:res.msg
              })
            }
          }).catch(error=>{})
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    handleChange(value){
      let length=value.length
      this.ruleForm.selectCityId=value[length-1]
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    initSelectData(){
      getCity().then(res=>{
        this.options=res.data
      }).catch(error=>{})
    }
  }
}
</script>

<style scoped>

</style>
